package nflparsing;

public class Player {
	
	public String _fullName;
	public int _playerID;
	public String _teamName;
	public int _teamID;
	public String _position;
	public String [] _fullString = new String[17];
	public String [] _injury = new String[17];
	public boolean [] _started = new boolean[17];
	public boolean [] _played = new boolean[17];
	public boolean [] _inactive = new boolean[17];
	public int [] _passingCompletions = new int[17];
	public int [] _passingAttempts = new int[17];
	public int [] _passingYards = new int[17];
	public int [] _passingInts = new int[17];
	public int [] _passingTDs = new int[17];
	public int [] _passingTwoPts = new int[17];
	public int [] _sacked = new int[17];
	public int [] _sackYardsLost = new int[17];
	public int [] _rushingAttempts = new int[17];
	public int [] _rushingYards = new int[17];
	public int [] _rushingTDs = new int[17];
	public int [] _rushingTwoPts = new int[17];
	public int [] _targets = new int[17];
	public int [] _receivingReceptions = new int[17];
	public int [] _receivingYards = new int[17];
	public int [] _receivingTDs = new int[17];
	public int [] _receivingTwoPts = new int[17];
	public int [] _kickingPATMade = new int[17];
	public int [] _kickingPATAttempt = new int[17];
	public int [] _kickingPATBlocked = new int[17];
	public int [] _kickingFGMade = new int[17];
	public int [] _kickingFGAttempt = new int[17];
	public int [] _kickingFGBlocked = new int[17];
	public int [] _kickingFGM_1_29 = new int[17];
	public int [] _kickingFGM_30_39 = new int[17];
	public int [] _kickingFGM_40_49 = new int[17];
	public int [] _kickingFGM_50p = new int[17];
	public int [] _fumblesLost = new int[17];
	public String [] _TDsByDist = new String[17];
	public String [] _TDsByDist2 = new String[17];
	/*****/
	public int [] _espnScore = new int[17];
	public float [] _nateEspnScore = new float[17];
	public int _ESPN;
	public float _ESPN_LATE_WEIGHT;
	public float _NATE_ESPN;
	public float _NATE_ESPN_LATE_WEIGHT;
	
	public int [] _yahooScore = new int[17];
	public float [] _nateYahooScore = new float[17];
	public int _YAHOO;
	public float _YAHOO_LATE_WEIGHT;
	public float _NATE_YAHOO;
	public float _NATE_YAHOO_LATE_WEIGHT;
	/*****/
	public boolean[] _puntReturns = new boolean[17];
	public int [] _puntReturnYards = new int[17];
	public int [] _puntReturnFairCatch = new int[17];
	public int [] _puntReturnTDs = new int[17];
	public int [] _kickoffReturns = new int[17];
	public int [] _kickoffReturnYards = new int[17];
	public int [] _kickoffReturnFairCatch = new int[17];
	public int [] _kickoffReturnTDs = new int[17];
	public int [] _blockedKicks = new int[17];
	public int [] _blockedKickRecoveries = new int[17];
	public int [] _blockedKickReturnYards = new int[17];
	public int [] _blockedKickReturnTDs = new int[17];
	//public int [] _fumblesLost;
	public int [] _fumblesForced = new int[17];
	public int [] _fumblesRecovered = new int[17];
	public int [] _fumblesReturnYards = new int[17];
	public int [] _fumblesReturnTDs = new int[17];
	public int [] _punts = new int[17];
	public int [] _puntsYards = new int[17];
	public int [] _puntsInside20 = new int[17];
	public int [] _puntsTouchback = new int[17];
	public int [] _puntsBlocked = new int[17];
	public int [] _kickoffs = new int[17];
	public int [] _kickoffsEndZone = new int[17];
	public int [] _kickoffsTouchBack = new int[17];
	/**********/
	public static int ESPN_SCORING = 0;
	public static int YAHOO_SCORING = 1;

	
	
	public static void main(String [] args) {
		System.out.println("hello nfl players");
		Player p = new Player();
		p._fullName = "DERP!";
		System.out.println(p);
	}
	
	public void calculateScores() {
		calculateESPNScore();
		calculateYahooScore();
		calculateNateScore();
	}
	
	private void calculateESPNScore() {
		_ESPN = 0;
		_ESPN_LATE_WEIGHT = 0;
		
		for (int i=0; i<17; ++i) {
			_espnScore[i] =
				//PASSING
				_passingYards[i] / 25 + _passingTDs[i] * 4 - _passingInts[i] * 2 + _passingTwoPts[i] * 2 + 
				(_passingYards[i] > 300 ? 2 : 0) + (_passingYards[i] > 400 ? 3 : 0) +
				//RECEIVING
				_receivingReceptions[i] + _receivingYards[i]/10 +  _receivingTDs[i] * 6 + _receivingTwoPts[i] * 2 +
				(_receivingYards[i] > 100 ? 2 : 0) + (_receivingYards[i] > 200 ? 3 : 0) +
				//RUSHING
				_rushingYards[i] / 10 + _rushingTDs[i] * 6 + _rushingTwoPts[i] * 2 +
				(_rushingYards[i] > 100 ? 2 : 0) + (_rushingYards[i] > 200 ? 3 : 0) +
				//MISC
				-_fumblesLost[i] * 2 + 2 * (_kickoffReturnYards[i] / 25) + 2 * (_puntReturnYards[i] / 25) +
				_kickoffReturnTDs[i] * 6 + _puntReturnTDs[i] * 6 + _fumblesReturnTDs[i] * 6 +
				//KICKING
				3*_kickingPATMade[i] - 2*_kickingPATAttempt[i] + 3*(_kickingFGM_1_29[i] + _kickingFGM_30_39[i]) +
				4*_kickingFGM_40_49[i] + 5*_kickingFGM_50p[i] + 
				//TDBonuses
				scoringBonuses(_TDsByDist[i], i, ESPN_SCORING);
			_ESPN += _espnScore[i];
			_ESPN_LATE_WEIGHT += _espnScore[i] * lateWeight(i);
		}	
	}
	
	private void calculateYahooScore() {
		_YAHOO = 0;
		_YAHOO_LATE_WEIGHT = 0;
		
		for (int i=0; i<17; ++i) {
			_yahooScore[i] =
				//PASSING
				_passingYards[i] / 25 + _passingTDs[i] * 6 - _passingInts[i] * 2 + _passingTwoPts[i] * 2 + 
				(_passingYards[i] > 300 ? 2 : 0)  +
				//RECEIVING
				_receivingReceptions[i] + _receivingYards[i]/10 +  _receivingTDs[i] * 6 + _receivingTwoPts[i] * 2 +
				(_receivingYards[i] > 100 ? 2 : 0) + (_receivingYards[i] > 200 ? 3 : 0) +
				//RUSHING
				_rushingYards[i] / 10 + _rushingTDs[i] * 6 + _rushingTwoPts[i] * 2 +
				(_rushingYards[i] > 150 ? 2 : 0) +
				//MISC
				-_fumblesLost[i] * 2 + (_kickoffReturnYards[i] / 20) + (_puntReturnYards[i] / 20) +
				_kickoffReturnTDs[i] * 6 + _puntReturnTDs[i] * 6 + _fumblesReturnTDs[i] * 6 +
				//KICKING
				3*_kickingPATMade[i] - 2*_kickingPATAttempt[i] + 3*(_kickingFGM_1_29[i] + _kickingFGM_30_39[i]) +
				4*_kickingFGM_40_49[i] + 5*_kickingFGM_50p[i] + 
				//TDBonuses
				scoringBonuses(_TDsByDist[i], i, YAHOO_SCORING);
			_YAHOO += _espnScore[i];
			_YAHOO_LATE_WEIGHT += _espnScore[i] * lateWeight(i);
		}	
	}
	
	private int scoringBonuses(String tdString, int weekNumber, int scoringType) {
		if (tdString == null) {
			return 0;
		} else {
			int bonus = 0;
			int index = 0;
			while (index < tdString.length()) {
				char type = tdString.charAt(index);
				switch(type) {
				case 'T':
				case 'R':
				case 'C':
					int tdLength = Integer.valueOf(tdString.substring(index+1, index+3));
					if (tdLength >= 40 && scoringType == ESPN_SCORING) {
						bonus += 2;
					}
					index += 4;
					break;
				case 'K':
				case 'P':
				case 'B':
				case 'I':
				case 'F':
				case 'O':
				case 'G':
				case 'M':
				case 'X':
					index += 4;
					break;
				case 'U':
					int fgLength = Integer.valueOf(tdString.substring(index+1, index+3));
					if (scoringType == ESPN_SCORING) {
						if (fgLength < 40) {
							bonus -= 2;
						} else if (fgLength < 50) {
							bonus -= 1;
						}
					} else if (scoringType == YAHOO_SCORING) {
						if (fgLength < 20) {
							bonus -= 2;
						} else if (fgLength < 30) {
							bonus += 1;
						}
					}
					index += 4;
					break;
				default:
					System.out.println("Unrecognized Scoring String: " + tdString);
					System.out.println("Full: " + _fullString[weekNumber]);
					if (_TDsByDist2[weekNumber] != null) {
						System.out.println("_TDsByDist2" + _TDsByDist2[weekNumber]);
					}
					return 0;
				}
			}
			return bonus;
		}
	}
	
	private float lateWeight(int i) {
		if (i < 6) {
			return 1.0F;
		} else if (i < 10) {
			return 1.5F;
		} else if (i < 16) {
			return 2.0F;
		} else {
			return 0F;
		}
	}
	
	private void calculateNateScore() {
		_NATE_ESPN = 0;
		_NATE_ESPN_LATE_WEIGHT = 0;
		
		for (int i=0; i<17; ++i) {
			_nateEspnScore[i] = (float) (_espnScore[i] + 
					(_targets[i] - _receivingReceptions[i]) * 0.25 - injuryScore(_injury[i]));
			_nateYahooScore[i] = (float) (_yahooScore[i] + 
					(_targets[i] - _receivingReceptions[i]) * 0.1 - injuryScore(_injury[i]));
			
			_NATE_ESPN += _nateEspnScore[i];
			_NATE_ESPN_LATE_WEIGHT += _nateEspnScore[i] * lateWeight(i);
			_NATE_YAHOO += _nateYahooScore[i];
			_NATE_YAHOO_LATE_WEIGHT += _nateYahooScore[i] * lateWeight(i);
		}
	}
	
	private float injuryScore(String injury) {
		if (injury == null) {
			return 0;
		} else if (injury.equalsIgnoreCase("O")) {
			return 5.0F;
		} else if (injury.equalsIgnoreCase("P")) {
			return 1.0F;
		} else if (injury.equalsIgnoreCase("Q")) {
			return 2.0F;
		} else {
			return 0;
		}
	}
	
	public Player() {}
	
	public String smallString() {
		return _fullName + (_fullName.length() < 12 ? "\t" : "") + ": \t" + _ESPN + ", \t" + _ESPN_LATE_WEIGHT + ", \t" + _NATE_ESPN + ",   \t" + _NATE_ESPN_LATE_WEIGHT +
			" | " + _YAHOO + ",\t" + _YAHOO_LATE_WEIGHT + ",\t" + _NATE_YAHOO + ",\t" + _NATE_YAHOO_LATE_WEIGHT;
	}
	
	public String toString() {
		String rez =  _fullName + ": " + _position + ", " + _teamName + "\n";
		if (_position.equalsIgnoreCase("RB") || _position.equalsIgnoreCase("QB")) {
			rez += "RUSHING YARDS: ";
			for (int ry : _rushingYards) {
				rez += ry + " ";
			}
			rez += "\nRUSHING TDS: ";
			for (int rt : _rushingTDs) {
				rez += rt + " ";
			}
			rez += "\n";
		}
		
		if (_position.equalsIgnoreCase("RB") || _position.equalsIgnoreCase("WR") ||
				_position.equalsIgnoreCase("TE")) {
			rez += "RECEIVING YARDS: ";
			for (int ry : _receivingYards) {
				rez += ry + " ";
			}
			rez += "\nRECEIVING TDS: ";
			for (int rt : _receivingTDs) {
				rez += rt + " ";
			}
			rez += "\n";
		}
		
		if (_position.equalsIgnoreCase("QB")) {
			rez += "PASSING YARDS: ";
			for (int py : _passingYards) {
				rez += py + " ";
			}
			rez += "\nPASSING TDS: ";
			for (int pt : _passingTDs) {
				rez += pt + " ";
			}
			rez += "\n";
		}
		
		return rez;
	}

}
